package GameEngine;

import java.util.ArrayList;

import Main.*;

/**
 * The controller which is generate the objects on the river.
 */
public class RiverController
{
	private String map;
	private ArrayList<RiverObject> riverObjects;
	
	
	public RiverController()
	{
		this.map = "";
		riverObjects = new ArrayList<RiverObject>();
		riverObjects.add(new BonusObject());
		riverObjects.add(new BlockingObject());
		riverObjects.add(new JumpObject());
		riverObjects.add(new HealObject());
	}
	
	/**
	 * If it is called on the first time it prepares an empty area on the river.
	 * After each call it gets a new random line of objects and adds it on map.
	 */
	public void initMap()
	{
		if (this.map=="")
		{
			for(int i=0;i<=11;i++)
			{
				//this.map +=this.getRandomRow();
				//initially empty
				this.map +="..........";
				if (i<11)
					this.map += ",";
			}
		}
		//Delete Last Row
		this.map = this.map.substring(0,this.map.lastIndexOf(",")+1);
		//Insert New Head Row
		this.map = this.getRandomRow()+","+this.map;
		
	}
	
	/**
	 * For the creation time for each new line of objects,
	 * It increases the score and level of the game
	 */
	public void renderMap(double overflow)
	{
		if (overflow==0)
		{
			this.initMap();
			Main.mygame.getBoat().setScore(Main.mygame.getBoat().getScore()+1);
			Main.mygame.setLevel(Main.mygame.getLevel()+1);
		}
	}
	
	/**
	 * Returns a random line of objects
	 */
	public String getRandomRow()
	{
		String myRow = "";
		for(int i=0;i<10;i++)
		{
			int myRand = (int) Math.floor(Math.random()*22);
			
			if(myRand>14 && myRand<17)	//Block Object
			{
				myRow +="#";
			}
			else if(myRand==17)
			{
				myRow +="_";
			}
			else if(myRand==20 && Main.mygame.getBoat().getHealthPercent()<100)	//Heal object
			{
				myRow +="+";
			}
			else if(myRand==21)	//Bonus object
			{
				//Reduces the chances for the higher point
				if (Math.floor(Math.random()*10)==9)
					myRow +=((int)Math.floor(Math.random()*9)+1)+"";
				else
					myRow +=((int)Math.floor(Math.random()*2)+1)+"";
			}
			else	//Water Object
			{
				myRow +=".";
			}
		}
		return myRow;
	}

	public String getMap() {
		return map;
	}

	public void setMap(String map) {
		this.map = map;
	}

	public ArrayList<RiverObject> getRiverObjects() {
		return riverObjects;
	}

	public void setRiverObjects(ArrayList<RiverObject> riverObjects) {
		this.riverObjects = riverObjects;
	}
	
}